package com.neusoft.resturant.controller;

import com.alibaba.fastjson2.JSON;
import com.neusoft.resturant.common.exception.ServiceException;
import com.neusoft.resturant.common.utils.R;
import com.neusoft.resturant.entity.Order;
import com.neusoft.resturant.entity.ResShop;
import com.neusoft.resturant.entity.SysUser;
import com.neusoft.resturant.mapper.ResShopMapper;
import com.neusoft.resturant.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("orders")
public class OrderController extends BaseController {
    @Autowired
    private IOrderService iOrderService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ResShopMapper resShopMapper;


    /**
     * 添加订单
     *
     * @param shopId  商店ID
     * @param session HttpSession对象
     * @return 返回添加订单的结果
     */
    @PostMapping("add")
    public R addOrder(Integer shopId, HttpSession session) {
        String token = (String) session.getAttribute("token");
        if (token == null) {
            return R.error("请先登录");
        }

        String userToString = (String) redisTemplate.opsForValue().get(token);
        SysUser sysUser = JSON.parseObject(userToString, SysUser.class);
        List<Order> order1 = iOrderService.selectOrder(sysUser.getUserName());
        if (order1 != null) {
            throw new ServiceException("订单已存在");
        }
        Order order = new Order();
        order.setCreateBy(sysUser.getUserName());
        order.setUpdateBy(sysUser.getUserName());
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setName(sysUser.getUserName());
        ResShop shop = resShopMapper.selectResShopById(Long.valueOf(shopId));
        if (shop == null) {
            throw new ServiceException("查询失败");

        }
        order.setShopId(shop.getId());
        order.setPic(shop.getCover());
        order.setDescription(shop.getDetail());
        System.out.println(sysUser.getUserId().toString());
        order.setUserId(Integer.valueOf(sysUser.getUserId().toString()));
        order.setCategory("待支付");
        iOrderService.insertOrder(order);
        order1 = iOrderService.selectOrder(sysUser.getUserName());
        System.out.println(order1);
        return R.success(order1);
    }

    @GetMapping("get")
    public R getOrder(HttpSession session) {
        String token = (String) session.getAttribute("token");
        String userToString = (String) redisTemplate.opsForValue().get(token);
        SysUser sysUser = JSON.parseObject(userToString, SysUser.class);
        List<Order> order3 = iOrderService.selectOrder(sysUser.getUserName());
        for (Order order : order3) {
            order.setUserId(null);
            order.setUpdateTime(null);
            order.setUpdateBy(null);
            order.setCreateTime(null);
            order.setCreateBy(null);
        }
        return R.success(order3);
    }

}
